var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "menuId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    }
};
var ztree;

//菜单ID
var menuId = T.p("menuId");
var parentId = T.p("parentId");
if (!parentId) {
    parentId = 0;
}
var vm = new Vue({
    el: '#rrapp',
    data: {
        title: "新增菜单",
        menu: {
            parentName: "",
            parentId: parentId,
            type: 1,
            orderNum: 0,
            icon: "",
            iconClass: "fa fa-fw"
        }
    },
    created: function () {
        if (menuId != null) {
            this.title = "修改菜单";
            this.getMenu(menuId)
        }

        //加载菜单树
        $.get("../sys/menu/select", function (r) {
            ztree = $.fn.zTree.init($("#menuTree"), setting, r.menuList);
            var node = ztree.getNodeByParam("menuId", vm.menu.parentId);
            ztree.selectNode(node);

            vm.menu.parentName = node.name;
        })
    },
    methods: {
        getMenu: function (menuId) {
            $.get("../sys/menu/info/" + menuId, function (r) {
                vm.menu = r.menu;
                vm.menu.iconClass = vm.menu.icon;
            });
        },
        saveOrUpdate: function (event) {
            var url = vm.menu.menuId == null ? "../sys/menu/save" : "../sys/menu/update";
            $.ajax({
                type: "POST",
                url: url,
                data: JSON.stringify(vm.menu),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.back();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        selectIcon: function () {
            parent.layer.open({
                type: 2,
                offset: '50px',
                skin: 'layui-layer-lan',
                title: "选择图标",
                area: ['1400px', '650px'],
                shade: 0,
                shadeClose: false,
                maxmin: true,
                content: '/comm/iconselect.html?icon=' + vm.menu.icon,
                btn: ['<i class="fa fa-close"></i> 关闭',
                    '<i class="fa fa-eraser"></i> 清除'],

                success: function (layero, index) {
                    var info = '<font color="red" class="pull-left mt10">提示：双击选择图标。</font>';
                    layero.find('.layui-layer-btn').append(info);
                },
                btn1: function (index, layero) {
                    var w = $(layero).find("iframe")[0].contentWindow;//通过该对象可以获取iframe中的变量，调用iframe中的方法
                    vm.menu.icon = w.$("#icon").val();
                    vm.menu.iconClass = vm.menu.icon;
                    parent.layer.close(index);
                },
                btn2: function (index, layero) {
                    vm.menu.icon = "";
                    vm.menu.iconClass = "fa fa-fw";
                }

            });
        },
        menuTree: function () {
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-lan',
                title: "选择菜单",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#menuLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级菜单
                    vm.menu.parentId = node[0].menuId;
                    vm.menu.parentName = node[0].name;

                    layer.close(index);
                }
            });
        },
        back: function (event) {
            history.go(-1);
        }
    }
});



